package com.mjh.convert;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.mjh.bean.DicValue;
import com.mjh.service.DicValueService;
import com.mjh.utils.ApplicationContextHolder;


import java.util.List;

public class IntentationStateConverter implements Converter<Integer> {

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 获取单元格内容
        String cellValue = cellData.getStringValue();
        // 从spring容器中获取指定的bean对象
        DicValueService dicValueService = ApplicationContextHolder.getBean(DicValueService.class);
        // 根据类型编码查询字典值集合
        List<DicValue> dicValueList = dicValueService.queryDicValueListByTypeCode("intentionState");
        // 循环遍历数据字典集合，判断编辑是否匹配
        DicValue dicValue = dicValueList.stream()
                .filter(dv -> cellValue.equals(dv.getTypeValue()))
                .toList().getFirst();
        // 判断是否有值
        if (ObjUtil.isNotEmpty(dicValue)) {
            return dicValue.getId();
        }
        return -1;
    }
}
